#include "goodswindow.h"
#include "ui_goodswindow.h"
#include "itemform.h"
#include <QListWidgetItem>
#include <QInputDialog>
#include <QFile>
#include <QJsonArray>
#include <QJsonDocument>
#include <QDateTime>
#include <QJsonObject>
#include <QtDebug>
#include <QMessageBox>
#include "adminwindow.h"

GoodsWindow::GoodsWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::GoodsWindow)
{
    ui->setupUi(this);
    this->setWindowTitle("仓库管理");


    goodsname << "苹果" << "香蕉" << "梨子" << "葡萄" << "康师傅红烧牛肉面" << "橙子" << "榴莲" << "五常大米" << "舒肤佳沐浴露";
    cost << "5" << "4" << "4" << "12" << "8" << "8" << "15" << "22" << "25";

    for(int i = 0; i < goodsname.size(); i++)
    {
        QListWidgetItem *item = new QListWidgetItem;

        ItemForm *form = new ItemForm;

        QString icon = QString(":/shop%1.jpg").arg(i+1);
        form->setinfo(icon,cost[i],goodsname[i]);
        item->setSizeHint(QSize(150,90));

        ui->buylistWidget->addItem(item);
        ui->buylistWidget->setItemWidget(item,form);
    }
}

GoodsWindow::~GoodsWindow()
{
    delete ui;
}

void GoodsWindow::on_buylistWidget_itemDoubleClicked(QListWidgetItem *item)
{
    QString num = QInputDialog::getText(this,"购买","输入购买数量");
    if(num == "")
    {
        QMessageBox::information(this,"提示","请规范输入信息");
        return;
    }
    num = num + "个/斤";
    //1.文件操作
    QFile file("./goods.json");
    QByteArray data;
    if(file.open(QIODevice::ReadOnly))
    {
        data = file.readAll();
        file.close();
    }
    QJsonDocument msg = QJsonDocument::fromJson(data);
    QJsonObject msg1 = msg.object();
    QJsonArray msg2 = msg1.value("data").toArray();

    QWidget *fn = ui->buylistWidget->itemWidget(item);

    QJsonObject obj1;
    obj1.insert("name",fn->findChild<QLabel*>("goodnamelabel")->text());
    obj1.insert("count",num);
    obj1.insert("time",QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd"));

    for(int i = 0;i < cost.size();i++)
    {
        QJsonObject arrayObject = msg2.at(i).toObject();
        if(fn->findChild<QLabel*>("goodnamelabel")->text() == goodsname.at(i))
        {

            int a[10];
            a[0] = cost[i].toInt();
            num.remove("个/斤");
            int num1 = num.toInt();
            int output = num1 * a[0];
            QString out = QString::number(output);
            obj1.insert("output",out);
            break;
        }
    }


    msg2.append(obj1);

    QJsonObject object;
    object.insert("data",msg2);

    QJsonDocument doc;
    doc.setObject(object);

    QByteArray data1 = doc.toJson();

    QFile file1("./goods.json");
    if(file1.open(QIODevice::WriteOnly))
    {
        file1.write(data1);
    }
    file1.close();

    QMessageBox::information(this,"提示","采购成功");
}

void GoodsWindow::on_pushButton_clicked()
{
    ui->plainTextEdit->clear();
    //1.文件操作
    QFile file("./goods.json");

    if(!file.open(QIODevice::ReadOnly))
    {
        qDebug() << "打开文件失败";
    }

    QByteArray array = file.readAll();

    //2.将QByteArray 转换成QJsonDocument
    QJsonDocument doc = QJsonDocument::fromJson(array);

   //3.根据json文件中的json格式，来获取doc中的对象或者数组
    QJsonObject object = doc.object(); //相当于get函数

    QJsonArray dataArray = object.value("data").toArray();

    for (int i=0;i < dataArray.size();i++)
    {

        QJsonObject arrayObject = dataArray.at(i).toObject();

        QString name = arrayObject.value("name").toString();
        ui->plainTextEdit->appendPlainText(name + "\r\n");

        QString count = arrayObject.value("count").toString();
        ui->plainTextEdit->appendPlainText(count + "\r\n");

        QString time = arrayObject.value("time").toString();
        ui->plainTextEdit->appendPlainText(time + "\r\n");

        QString output = arrayObject.value("output").toString();
        output = "花费金额:" + output;
        ui->plainTextEdit->appendPlainText(output + "\r\n");
        ui->plainTextEdit->appendPlainText("\r\n");

    }


}

void GoodsWindow::on_returnButton_clicked()
{
    AdminWindow *aw = new AdminWindow;
    aw->show();
    this->close();
}
